package com.cqyc.shop.controller;

import java.io.IOException;
import java.math.BigDecimal;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqyc.shop.comm.commException;
import com.cqyc.shop.domain.Cart;
import com.cqyc.shop.services.CartServices;
@WebServlet(urlPatterns="/addcart.do")
public class AddCartServlet  extends HttpServlet{
	private static final long serialVersionUID = 13794490357076048L;
	private Logger logger  = LoggerFactory.getLogger(getClass());
	private CartServices cartServices = new CartServices();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("增加购物车中商品");
		try {
			//1:从前台获取参数
			String goodsId = request.getParameter("goodsId");
			String prices = request.getParameter("prices");
			Cart cart = new Cart();
			BigDecimal prices2 = new BigDecimal(prices);
			
			cart.setPrice(prices2);
			logger.debug("{}",goodsId);
			//2:数据转换
			Integer  idd = Integer.valueOf(goodsId);
			cart.setId(idd);
			//3:数据校验
			if(idd == null) {
				throw new commException("添加商品失败");
			}
			cartServices.update(cart);
			//4:业务处理
			//5:跳转
			logger.debug("数量增加成功");
			response.sendRedirect(request.getContextPath()+"/showCart.jsp");
		} catch (Exception e) {
			throw new commException("添加商品数量失败");
		}
	}
	
}
